package com.cmd;

import com.alibaba.fastjson.JSONObject;
import com.mgr.DriverOrderMgr;
import com.mgr.UserOrderMgr;
import com.player.User;
import com.qy.bean.OrderDriver;
import com.qy.bean.OrderInfo;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.BaoShangCarUtil;
import com.util.PBMessage;

/**
 * 订单扫码
 * 
 * @author zwl 2018年2月7日
 */
public class CheckQRCodeCMD extends AbstractCommand {

    @Override
    public void execute(User user, PBMessage packet) throws Exception {
	JSONObject reJs = new JSONObject();
	JSONObject param = packet.getMessage();
	if (!param.containsKey("qrid")) {
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.param_error.getType());
	    user.sendPacket(reJs);
	    return;
	}
	String qrid = param.getString("qrid");
	// 订单信息
	OrderInfo info = BaoShangCarUtil.ImageNameToOrder(qrid);
	if (info == null
		|| info.getOrderStatus() != StaticData.OrderStatus.ARRIVE_NOT_CHECK.getType()
		|| info.getOrderStatus() != StaticData.OrderStatus.START_NOT_ARRIVE_STATION.getType()
		|| info.getOrderStatus() != StaticData.OrderStatus.ACCEPT_NOT_CHECK.getType()
		|| info.getOrderStatus() != StaticData.OrderStatus.ARRIVE_COUNT_DOWN.getType()) {
	    reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.QR_ERROR.getType());
	    user.sendPacket(reJs);
	    return;
	}
	/*
	 * 扫码有四种情况：
	 *  1.取车的时候司机扫车主 
	 *  2.到达检车站,检车员扫司机的 
	 *  3.检车完毕，还车司机扫检车员 
	 *  4.还车司机扫，车辆接收人
	 */
	int reCode = StaticData.no_param;
	OrderDriver beforeOD = null;
	OrderDriver od = null;
	if (info.getOrderStatus() == StaticData.OrderStatus.ARRIVE_NOT_CHECK.getType()) {
	    /*
	     * 取车的时候司机扫车主 判断司机是否是对应的司机 修改订单状态
	     */
	    od = DriverOrderMgr.getDriverOrder(info.getFDriver());
	    reCode = DriverOrderMgr.startService(od, user.getID());
	    if (reCode != StaticData.codeType.ok.getType()) {
		reJs.put(StaticData.reKey.code.getKey(), reCode);
		user.sendPacket(reJs);
		return;
	    }
	    info.setOrderStatus(StaticData.OrderStatus.CHECK_NOT_START.getType());
	} else if (info.getOrderStatus() == StaticData.OrderStatus.START_NOT_ARRIVE_STATION
		.getType()) {
	    /*
	     * 到达检车站,
	     * 检车员扫司机的
	     */
	    beforeOD = DriverOrderMgr.getDriverOrder(info.getFDriver());
	    od = DriverOrderMgr.createOd(user.getID(), info);
	    reCode = DriverOrderMgr.startService(od, user.getID());
	    if (reCode != StaticData.codeType.ok.getType()) {
		reJs.put(StaticData.reKey.code.getKey(), reCode);
		user.sendPacket(reJs);
		return;
	    }
	    // 结算之前的服务订单
	    reCode = DriverOrderMgr.settle(beforeOD);
	    if (reCode != StaticData.codeType.ok.getType()) {
		reJs.put(StaticData.reKey.code.getKey(), reCode);
		user.sendPacket(reJs);
		return;
	    }
	    
	    info.setOrderStatus(StaticData.OrderStatus.NOT_COMPLET_CHECK.getType());
	} else if (info.getOrderStatus() == StaticData.OrderStatus.ACCEPT_NOT_CHECK.getType()) {
	    /*
	     * 检车完毕，还车员扫检车员的码
	     */
	    beforeOD = DriverOrderMgr.getDriverOrder(info.getSDriver());
	    od = DriverOrderMgr.getDriverOrder(info.getTDriver());
	    reCode = DriverOrderMgr.startService(od, user.getID());
	    if (reCode != StaticData.codeType.ok.getType()) {
		reJs.put(StaticData.reKey.code.getKey(), reCode);
		user.sendPacket(reJs);
		return;
	    }
	    // 结算之前的服务订单
	    reCode = DriverOrderMgr.settle(beforeOD);
	    if (reCode != StaticData.codeType.ok.getType()) {
		reJs.put(StaticData.reKey.code.getKey(), reCode);
		user.sendPacket(reJs);
		return;
	    }
	    info.setOrderStatus(StaticData.OrderStatus.CHECK_NOT_ARRIVE.getType());
	}else if (info.getOrderStatus() == StaticData.OrderStatus.ARRIVE_COUNT_DOWN.getType()) {
	    /*
	     * 车主
	     */
	    od = DriverOrderMgr.getDriverOrder(info.getTDriver());
	    if (od.getDriverId() != user.getID()) {
		reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.NOT_THIS_DRIVER.getType());
		user.sendPacket(reJs);
		return;
	    }
	    // 结算之前的服务订单
	    reCode = DriverOrderMgr.settle(od);
	    if (reCode != StaticData.codeType.ok.getType()) {
		reJs.put(StaticData.reKey.code.getKey(), reCode);
		user.sendPacket(reJs);
		return;
	    }
	    info.setOrderStatus(StaticData.OrderStatus.USER_CHECK_FINISH.getType());
	}
	UserOrderMgr.update(info);
	reJs.put(StaticData.reKey.code.getKey(),StaticData.codeType.ok.getType());
	reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
	user.sendPacket(reJs);
    }

}
